import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:hairen_app/pages/user/viewHistory.dart';
import 'package:hairen_app/pages/user/version.dart';
import 'package:hairen_app/pages/user/favorites.dart';
import 'package:hairen_app/server/saveInfo.dart';
import 'package:hairen_app/pages/plug/func.dart';
import 'package:hairen_app/main.dart';
import 'package:hairen_app/pages/player/webview.dart';
import 'package:hairen_app/server/state/UserInfoModel.dart';



class DrawerPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return new DrawerPageState();
  }
}

class DrawerPageState extends State<DrawerPage> {
  Map userInfo={};
  String token="";
  getInfo(){
    SaveInfo.getUserToken().then((Map ret){
      setState(() {
        userInfo=ret['userInfo'];
        token=ret['token'];
        print(userInfo);
      });
    });
  }
  @override
  void initState() {
    // TODO: implement initState
    getInfo();
    super.initState();

  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return   new Drawer(
      child: new Column(
        children: <Widget>[
          new UserAccountsDrawerHeader(
            decoration: new BoxDecoration(
                image: new DecorationImage(
                    image: AssetImage('assets/drawerBg.jpg'))),
            accountName: new Text(userInfo.isEmpty?"你好请登陆":"${userInfo['user_nickname']==""?userInfo['user_email']:userInfo['user_nickname']}",style: new TextStyle(color: Colors.white)),
            accountEmail: new Text("欢迎观看",style: new TextStyle(color: Colors.white)),
            currentAccountPicture: new CircleAvatar(
              backgroundImage: AssetImage('assets/default_user.png'),
              radius: 35.0,
            ),
          ),
          new ListTile(
            leading: new Icon(Icons.refresh),
            title: new Text("首页"),
            onTap: () {
              Navigator.of(context).push(
                  new CupertinoPageRoute(builder: (context)=>new MyHomePage(currentIndex:0))
              );
            },
          ),
          new ListTile(
            leading: new Icon(Icons.refresh),
            title: new Text("最近观看"),
            onTap: () {
              Navigator.of(context).push(
                new CupertinoPageRoute(
                    builder: (context) => new ViewHistory()),
              );
            },
          ),
          new ListTile(
            leading: new Icon(Icons.favorite_border),
            title: new Text("我的收藏"),
            onTap: () {
              Navigator.of(context).push(
                new CupertinoPageRoute(
                    builder: (context) => new Favorites()),
              );


            },
          ),
          new ListTile(
            leading: new Icon(Icons.cached),
            title: new Text("版本更新"),
            onTap: () {
              Navigator.of(context).push(
                new CupertinoPageRoute(builder: (context) => new Version()),
              );
            },
          ),
          new ListTile(
            leading: new Icon(Icons.settings),
            title: new Text("设置"),
            onTap: () {
//              Navigator.of(context).push(
//                new CupertinoPageRoute(builder: (context) => new WebView()),
//              );
              Navigator.of(context).pushNamed('/widget');
            },
          ),
          new ListTile(
            leading: new Icon(Icons.exit_to_app),
            title: new Text("退出登陆"),
            onTap: () {
              SaveInfo.clearToken().then((ret) {
                func.alertConfirm(context, "退出成功！", (ret) {
                  UserInfoModel.isLogin=false;
                  Navigator.of(context).push(
                      new CupertinoPageRoute(builder: (context)=>new MyHomePage(currentIndex:0))
                  );
                });
              });
              SaveInfo.clearUserInfo().then((ret) {});
            },
          ),
        ],
      ),
    );
    }
}
